package com.lee.forum.extension;

import com.lee.forum.common.AppResult;
import com.lee.forum.model.Article;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2024-03-09
 * Time: 18:44
 */
public class Extensions {

    // 带有分页功能的文章列表页
    @ApiOperation("获取有效帖子列表(分页)")
    @RequestMapping("/getlistbypage")
    public AppResult<List<Article>> getListByPage(@ApiParam("板块Id") @RequestParam(value = "boardId",required = false) Long boardId,
                                                  @ApiParam("页号") @RequestParam(value = "pIndex",required = false) Integer pIndex,
                                                  @ApiParam("每页大小") @RequestParam(value = "pSize",required = false) Integer pSize) {
        // 1.参数矫正
        if (pIndex == null || pIndex < 1) {
            pIndex = 1;
        }
        if (pSize == null || pSize < 10) {
            pSize = 10;
        }

        // 2.确定limit和offset
        int finalLimit = pSize;
        int finalOffset = pSize*(pIndex - 1);

        // 分页思路：select ... from articles where boardId = ? limit finalLimit offset finalOffset
        // 3.定义结果集
        List<Article> articles = null;
        if (boardId == null) {
            // 1.如果boardId为null,即未传参数，默认获取首页帖子
            // 调用service查询所有帖子
            // TODO

        } else {
            // 2.如果boardId不为null，则获取指定板块下的帖子
            // TODO
        }
        // 4.判断帖子是否有效
        if (articles == null) {
            articles = new LinkedList<>();
        }

        // 5.返回结果
        return AppResult.success(articles);
    }
}
